import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import java.util.regex.*;

public class ParserHandler extends DefaultHandler {

	String type, status, code, desc, from, media;
	
	public void clear() {
		type = status = code = desc = from = media = "";
	}
	
	public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
		if(localName.equals("return")) {
			type = atts.getValue("type");
			status = atts.getValue("status");
			code = atts.getValue("code");
			desc = atts.getValue("desc");
		} else if (localName.equals("call")) {
			type = "call";
			from = this.findAndReplace(atts.getValue("from"));
			media = atts.getValue("media");
		} else if (localName.equals("ringing")) {
			type = "ringing";
		} else if (localName.equals("accept")) {
			type = "accept";
			media = atts.getValue("media");
		} else if (localName.equals("cancel")) {
			type = "cancel";
			from = this.findAndReplace(atts.getValue("from"));
		} else if (localName.equals("bye")) {
			type = "bye";
			from = this.findAndReplace(atts.getValue("from"));
		}
	}

	public void characters(char[] ch, int start, int length) throws SAXException {

	}
 
	public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
	
	}
	
	public String findAndReplace(String data) {
		data = replace(data, "&lt;", "<");
		data = replace(data, "&gt;", ">");
		data = replace(data, "&apos;", "'");
		data = replace(data, "&quot;", "\"");
		data = replace(data, "&amp;", "&");
		return data;
	}
	
	public String replace(String data, String patternStr, String replacementStr) {
		// Compile regular expression
		Pattern pattern = Pattern.compile(patternStr);
    
		// Replace all occurrences of pattern in input
		Matcher matcher = pattern.matcher(data);
		String output = matcher.replaceAll(replacementStr);
		return output;
	}
}